package com.rjkj.medicalwaste.ui.storage

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.rjkj.medicalwaste.MainActivity
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.base.initTip
import com.rjkj.medicalwaste.base.showTip
import com.rjkj.medicalwaste.databinding.DialogFixedCodeBinding
import com.rjkj.medicalwaste.databinding.DialogUserCodeBinding
import com.rjkj.medicalwaste.livedatabus.LiveDataBus
import com.rjkj.medicalwaste.utils.StringUtils
import com.rjkj.medicalwaste.viewmodels.UnBoxedViewModel
import com.rjkj.medicalwaste.viewmodels.UserViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class ScanUserDialog : DialogFragment() {
    var callBack: Callback? = null
    private val userViewModel: UserViewModel by activityViewModels {
        MainViewModelProvider.provideUserModel(requireContext())
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = DialogUserCodeBinding.inflate(inflater, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        onSubscribeUi(binding)
        return binding.root
    }

    fun onSubscribeUi(binding: DialogUserCodeBinding) {
        binding.imClose.setOnClickListener {
            callBack?.onGetCode("1111111")
            Log.e("DialogUserCodeBinding", "onSubscribeUi: ${viewLifecycleOwner.lifecycle.currentState.toString()}", )
//            dismiss()
        }
//        LiveDataBus.get().with(MainActivity.SCAN_DATA, String::class.java).observe(
//            viewLifecycleOwner,
//        ) {
//            userViewModel.checkLogin(it).observe(viewLifecycleOwner, { user ->
//                if (user != null) {
//                    binding.tvName.text = user.name
//                    callBack?.onGetCode(user.qrcode)
//                    dismiss()
//                } else {
//                    binding.tvName.text = "未找到清理人"
//                }
//            })
//        }

    }

    interface Callback {
        fun onGetCode(code: String)
    }

    override fun onStart() {
        dialog!!.window!!.apply {
            attributes.gravity = Gravity.CENTER//位于底部
            decorView.background = ColorDrawable(Color.TRANSPARENT)//背景色
        }
        super.onStart()
    }


}